package com.cxp.serverdemo.service.serviceImpl;

import com.cxp.serverdemo.Repository.MenuRepository;
import com.cxp.serverdemo.Repository.RoleRepository;
import com.cxp.serverdemo.bean.sys.bean.Menu;
import com.cxp.serverdemo.bean.sys.bean.Role;
import com.cxp.serverdemo.service.MenuService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
public class MenuServiceImpl implements MenuService {
    private static final Logger logger = LoggerFactory.getLogger(MenuServiceImpl.class);

    @Autowired
    MenuRepository menuRepository;
    @Autowired
    RoleRepository roleRepository;

    @Override
    public List<Menu> getAllMenu() {
        List<Menu> menus = menuRepository.findAll();
        logger.info("getAllMenu.menus1-------" + menus.get(0).getRoles().size());
        for(Menu menu:menus){
            logger.info("getAllMenu.menu.id-------" + menu.getId().toString());
            List<Role> roles = roleRepository.findRolesByMenuId(menu.getId().longValue());
            logger.info("getAllMenu.roles1-------" + roles.toString());
            if(!roles.isEmpty()){
                logger.info("getAllMenu.roles2-------" + roles.toString());
                menu.setRoles(roles);
            }
        }
        logger.info("getAllMenu.menus2-------" + menus.get(0).getRoles().size());
        return menus;
    }
}
